package com.superbytecode.stencil.utils;

import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: code-stencil
 * @Package: com.superbytecode.stencil.utils
 * @ClassName: StringUtil
 * @Description: [字符串工具类]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/10/11 14:52
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.cn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@UtilityClass
public class StringUtil extends StringUtils {

    public String concatPath(String... modules) {
        return String.join(File.separator, modules);
    }

    public boolean equalsIgnoreCase(String target1, String target2, String source1, String source2) {
        return equalsIgnoreCase(target1, source1) && equalsIgnoreCase(target2, source2);
    }

    /**
     * 移除首字母前/尾字母后的的特殊字符
     * <p>
     * "__user__" -> "user"
     * </p>
     *
     * @param value   字符串
     * @param setting 特殊字符
     * @return 移除后的字符串
     */
    public String trim(String value, char setting) {
        value = trimLeading(value, setting);
        value = trimTrailing(value, setting);
        return value;
    }

    /**
     * 移除首字母前的特殊字符
     * <p>
     * "__name__" -> "name__"
     * </p>
     *
     * @param value   字符串
     * @param leading 首字母前的特殊字符
     * @return 移除后的字符串
     */
    public String trimLeading(String value, char leading) {
        if (isEmpty(value)) return value;
        int beginIdx;
        for (beginIdx = 0; beginIdx < value.length() - 1; beginIdx++) {
            if (!Objects.equals(leading, value.charAt(beginIdx))) {
                break;
            }
        }
        return value.substring(beginIdx);
    }

    /**
     * 移除尾字母后的特殊字符
     * <p>
     * "__name__" -> "__name"
     * </p>
     *
     * @param str      字符串
     * @param trailing 尾字母后的特殊字符
     * @return 移除后的字符串
     */
    public String trimTrailing(String str, char trailing) {
        if (isEmpty(str)) return str;
        int endIdx;
        for (endIdx = str.length() - 1; endIdx >= 0; endIdx--) {
            if (!Objects.equals(trailing, str.charAt(endIdx))) {
                break;
            }
        }
        return str.substring(0, endIdx + 1);
    }

    /**
     * 首字母前或未字母后的所有指定特殊字符个数
     * <p>
     * case1 false:   "_user_name__" -> 1
     * case2 true:    "_user_name__" -> 2
     * </p>
     *
     * @param value   字符串
     * @param setting 指定字符，默认"_"
     * @param revers  是否翻转，false-首字目前，true-尾字目个数
     * @return
     */
    @SuppressWarnings("all")
    public int findSettingOffset(String value, char setting, boolean revers) {
        int settingCharCount = 0; // 特殊字符个数
        if (isEmpty(value)) {
            return settingCharCount;
        }
        String targetValue = revers ? new StringBuffer(value).reverse().toString() : value;
        for (char c : targetValue.toCharArray()) {
            if (c == setting) {
                settingCharCount++;
            } else {
                break;
            }
        }
        return settingCharCount;
    }

    /**
     * 将第一个字母转换成大写，非首字母全部小写
     * <p>
     * "naMe" -> "Name"
     * </p>
     *
     * @param letter 字符串
     * @return 首字母大写后的字符串
     */
    public String capitalizeFirstLetter(String letter) {
        return isEmpty(letter) ? letter : Character.toUpperCase(letter.charAt(0)) + letter.substring(1).toLowerCase();
    }

    /**
     * 将第一个字母转换成小写
     * <p>
     * "NaMe" -> "naMe"
     * </p>
     *
     * @param letter 字符串
     * @return 首字母小写后的字符串
     */
    public String lowercaseFirstLetter(String letter) {
        return isEmpty(letter) ? letter : Character.toLowerCase(letter.charAt(0)) + letter.substring(1);
    }

    /**
     * 将第一个字母转换成大写
     * <p>
     * "naMe" -> "NaMe"
     * </p>
     *
     * @param letter 字符串
     * @return 首字母大写后的字符串
     */
    public String uppercaseFirstLetter(String letter) {
        return isEmpty(letter) ? letter : Character.toUpperCase(letter.charAt(0)) + letter.substring(1);
    }

    /**
     * 过滤分解"."，并首字母大写
     *
     * @param letter 字段
     * @return 返回新字段
     */
    public String dotFilter(String letter) {
        if (isEmpty(letter) || !letter.contains(".")) return letter;
        StringBuilder ret = new StringBuilder();
        for (String str : letter.split("\\.")) {
            ret.append(uppercaseFirstLetter(str));
        }
        return ret.toString();
    }

    /**
     * 获取字符串第一个词
     *
     * @param word   字符串
     * @param splits 分隔符
     * @return 第一个词
     */
    public String filterFirstKeyword(String word, String... splits) {
        if (StringUtil.isBlank(word) || ArrayUtils.isEmpty(splits)) {
            return word;
        }
        for (String split : Arrays.stream(splits).filter(StringUtil::isNotEmpty).collect(Collectors.toList())) {
            if (!word.contains(split)) {
                continue;
            }
            word = word.substring(0, word.indexOf(split));
        }
        return word;
    }
}